package com.atguigu.flink.chapter08;

import com.atguigu.flink.bean.AdsClickLog;
import org.apache.flink.api.common.eventtime.SerializableTimestampAssigner;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.common.state.ReducingState;
import org.apache.flink.api.common.state.ReducingStateDescriptor;
import org.apache.flink.api.common.state.ValueState;
import org.apache.flink.api.common.state.ValueStateDescriptor;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.KeyedProcessFunction;
import org.apache.flink.util.Collector;

import java.text.SimpleDateFormat;
import java.time.Duration;

/**
 * @Author lizhenchao@atguigu.cn
 * @Date 2022/1/14 8:56
 */
public class Flink04_High_Project_Ads {
    public static void main(String[] args) {
        Configuration conf = new Configuration();
        conf.setInteger("rest.port", 20000);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);
        env.setParallelism(1);
        
        env
            .readTextFile("input/AdClickLog.csv")
            .map(new MapFunction<String, AdsClickLog>() {
                @Override
                public AdsClickLog map(String value) throws Exception {
                    String[] data = value.split(",");
                    return new AdsClickLog(
                        Long.valueOf(data[0]),
                        Long.valueOf(data[1]),
                        data[2],
                        data[3],
                        Long.valueOf(data[4]) * 1000
                    );
                }
            })
            .assignTimestampsAndWatermarks(
                WatermarkStrategy
                    .<AdsClickLog>forBoundedOutOfOrderness(Duration.ofSeconds(3))
                    .withTimestampAssigner(new SerializableTimestampAssigner<AdsClickLog>() {
                        @Override
                        public long extractTimestamp(AdsClickLog element,
                                                     long recordTimestamp) {
                            return element.getTimestamp();
                        }
                    })
            )
            .keyBy(log -> log.getUserId() + "_" + log.getAdsId())
            .process(new KeyedProcessFunction<String, AdsClickLog, String>() {
    
                private ValueState<String> yesterdayState;
                private ValueState<Boolean> warnState;
                private ReducingState<Long> countState;
                
                @Override
                public void open(Configuration parameters) throws Exception {
                    countState = getRuntimeContext()
                        .getReducingState(new ReducingStateDescriptor<Long>(
                            "countState",
                            Long::sum,
                            Long.class
                        ));
                    warnState = getRuntimeContext().getState(new ValueStateDescriptor<Boolean>("warnState", Boolean.class));
    
                    yesterdayState = getRuntimeContext().getState(new ValueStateDescriptor<String>("yesterdayState", String.class));
    
                }
                
                @Override
                public void processElement(AdsClickLog log,
                                           Context ctx,
                                           Collector<String> out) throws Exception {
                    // 如何判断数据跨天: 今天的年月日和昨天的年月日不一样
                    String yesterday = yesterdayState.value();  // 如果是第一次运行, 第一条数据进来, 则这个值是null
                    String today = new SimpleDateFormat("yyyy-MM-dd").format(log.getTimestamp());
                    if (!today.equals(yesterday)) {
                        // 跨天或者是第一条数据
                        countState.clear();
                        warnState.clear();
                        yesterdayState.update(today);
                        
                    }
                    
                    
                    
                    if (warnState.value() == null) {  // 如果已经加入黑名单, 则不需要累计
                        countState.add(1L);
                    }
                    
                    Long count = countState.get();
                    String msg = "用户: " + log.getUserId() + " 对广告: " + log.getAdsId() + " 的点击量是: " + count;
                    if (count > 99) {
                        if (warnState.value() == null) {
                            msg += "超过阈值100, 加入黑名单";
                            out.collect(msg);
                            warnState.update(true);
                        }
                    } else {
                        out.collect(msg);
                    }
                }
            })
            .print();
        
        try {
            env.execute();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
